<template>
  <div>
    <div class="login">
      <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import {getLogin} from "../utlis/api"
const ruleFormRef = ref<FormInstance>()
const router = useRouter()
const route = useRoute()
const store = useStore()
const ruleForm = reactive({
  email: '2110410005@qq.com',
  password: 'liu129499',
})
const rules = reactive({
  email: [{ required: true, message: '电子邮箱地址不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
})
// 登录
const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
          getLogin(ruleForm).then(res=>{
            console.log(43,res);
            store.commit("login",res.data.token)
            setTimeout(() => {
              store.commit("login",res.data.token)
            }, 1000);
            router.push({path:'/'})
          })
        } else {
            console.log('error submit!')
            return false
        }
    })
}
</script>

<style scoped lang='scss'>
.login {
  width: 500px;
  height: 400px;
  margin: 200px auto;
}
</style>